package org.chen.aao.user.controller;

import org.chen.aao.common.exception.BusinessException;
import org.chen.aao.common.response.Result;
import org.chen.aao.common.user.dto.*;
import org.chen.aao.user.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
public class UserController {
    
    private final UserService userService;
    
    @PostMapping("/register")
    public Result<RegisterResponse> register(@Validated @RequestBody RegisterRequest request) {
        return Result.success(userService.register(request));
    }

    /**
     * 发送注册验证码
     */
    @PostMapping("/send-register-code")
    public Result<?> sendRegisterCode(@Validated @RequestBody SendCodeRequest request) {
        userService.sendRegisterCode(request);
        return Result.success("验证码已发送到您的邮箱");
    }

    /**
     * 用户登录
     */
    @PostMapping("/login")
    public Result<LoginResponse> login(@Validated @RequestBody LoginRequest request) {
        return Result.success(userService.login(request));
    }

    /**
     * 发送密码重置验证码
     */
    @PostMapping("/send-reset-code")
    public Result<?> sendResetPasswordCode(@Validated @RequestBody SendCodeRequest request) {
        userService.sendResetPasswordCode(request);
        return Result.success("验证码已发送到您的邮箱");
    }

    /**
     * 重置密码
     */
    @PostMapping("/reset-password")
    public Result<?> resetPassword(@Validated @RequestBody ResetPasswordRequest request) {
        userService.resetPassword(request);
        return Result.success("密码重置成功");
    }
    
    @GetMapping("/info")
    public Result<UserInfoResponse> getUserInfo(HttpServletRequest request) {
        Long userId = getUserIdFromRequest(request);
        return Result.success(userService.getUserInfo(userId));
    }

    /**
     * 更新用户资料
     */
    @PutMapping("/profile")
    public Result<UserInfoResponse> updateProfile(HttpServletRequest request,
                                                  @RequestBody UpdateProfileRequest updateRequest) {
        Long userId = getUserIdFromRequest(request);
        return Result.success(userService.updateProfile(userId, updateRequest));
    }

    /**
     * 修改密码
     */
    @PostMapping("/change-password")
    public Result<?> changePassword(HttpServletRequest request,
                                    @Validated @RequestBody ChangePasswordRequest changeRequest) {
        Long userId = getUserIdFromRequest(request);
        userService.changePassword(userId, changeRequest);
        return Result.success("密码修改成功");
    }

    /**
     * 刷新Token
     */
    @PostMapping("/refresh-token")
    public Result<RefreshTokenResponse> refreshToken(@Validated @RequestBody RefreshTokenRequest request) {
        return Result.success(userService.refreshToken(request));
    }

    /**
     * 登出
     */
    @PostMapping("/logout")
    public Result<?> logout(HttpServletRequest request) {
        Long userId = getUserIdFromRequest(request);
        userService.logout(userId);
        return Result.success("登出成功");
    }

    /**
     * 从请求中获取用户ID
     */
    private Long getUserIdFromRequest(HttpServletRequest request) {
        // 从请求头中获取用户ID（通过拦截器设置）
        String userIdHeader = request.getHeader("X-User-Id");
        if (userIdHeader == null || userIdHeader.trim().isEmpty()) {
            throw new BusinessException("用户未登录");
        }
        try {
            return Long.valueOf(userIdHeader);
        } catch (NumberFormatException e) {
            throw new BusinessException("用户ID格式错误");
        }
    }
}